//
//  AudiovisualViewController.m
//  NEWS_W_Y_S
//
//  Created by lanou3g on 15/10/14.
//  Copyright © 2015年 lanou3g.jake. All rights reserved.
//

#import "AudiovisualViewController.h"
#import "VideoTableViewController.h"
#import "RadioCollectionViewController.h"
#import "VideoTableViewCell.h"

@interface AudiovisualViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UISegmentedControl *videoAndRadioSegment;

@property(nonatomic,strong) UIScrollView *scrollView;

@property(nonatomic,strong) VideoTableViewController *videoVC;

@property(nonatomic,assign) BOOL isPlay;

@property(nonatomic,strong) RadioCollectionViewController *radioVC;

@end


static NSInteger index1 = 0;

@implementation AudiovisualViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isPlay = NO;
    //
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height)];
      self.automaticallyAdjustsScrollViewInsets = NO; //当我们一个界面有多个tableView之类的,要将它设置为NO,完全由自己手动来布局,就不会错乱了.
    self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, 0);// 可现实的区域尺寸
    self.scrollView.pagingEnabled = YES;//整页翻动
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    //头部的segment分段控件
    NSArray *array = @[@"视频",@"电台"];
    self.videoAndRadioSegment = [[UISegmentedControl alloc]initWithItems:array];
    self.videoAndRadioSegment.frame = CGRectMake(100, 10, 170, 30);
    self.videoAndRadioSegment.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
    self.videoAndRadioSegment.layer.masksToBounds = YES;
    self.videoAndRadioSegment.layer.cornerRadius = 5;
    //self.videoAndRadioSegment.segmentedControlStyle = UISegmentedControlStyleBar; //这个方法可能弃用的
    self.navigationItem.titleView = self.videoAndRadioSegment;
    self.videoAndRadioSegment.selectedSegmentIndex = 0;
   // [self.videoAndRadioSegment setTitle:@"视频" forSegmentAtIndex:0];
    self.videoAndRadioSegment.tintColor = [UIColor whiteColor];
    [self.videoAndRadioSegment addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged];
   //添加2个控制器
    [self loayoutViewVC];
    
    
}

-(void)changeView:(UISegmentedControl *)segmentControl
{
    if (self.videoAndRadioSegment.selectedSegmentIndex == 0) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
     //   self.scrollView.backgroundColor  = [UIColor redColor];
    }
    else{
        self.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width, 0);
     //   self.scrollView.backgroundColor = [UIColor greenColor];
    }
}

-(void)loayoutViewVC{
    //加载视频控制器
    self.videoVC = [[VideoTableViewController alloc] init];
    self.videoVC.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 95);
    [self addChildViewController:self.videoVC];
    [self.scrollView addSubview:self.videoVC.tableView];
    //加载电台控制器
    UICollectionViewFlowLayout *laout = [[UICollectionViewFlowLayout alloc] init];
    self.radioVC = [[RadioCollectionViewController alloc] initWithCollectionViewLayout:laout];
    [self addChildViewController:self.radioVC];
    self.radioVC.laout = laout;
    self.radioVC.collectionView.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.radioVC.view.frame.size.height - 95);
    [self.scrollView addSubview:self.radioVC.collectionView];
    
}

#pragma mark -- scrollView 的代理
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index2 = self.scrollView.contentOffset.x / self.view.bounds.size.width;
    self.videoAndRadioSegment.selectedSegmentIndex = self.scrollView.contentOffset.x / self.view.bounds.size.width;
    if (index1 != index2) {
        
    }
    index1 = index2;
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    
}





















@end
